/*
This file is part of Arcadeflex.

Arcadeflex is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Arcadeflex is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Arcadeflex.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * ported to v0.28
 * ported to v0.27
 *
 *  roms are from v0.36 romset
 *
 */




package drivers;

import static arcadeflex.libc.*;
import static mame.commonH.*;
import static mame.cpuintrf.*;
import static mame.driverH.*;
import static mame.mame.*;
import static mame.inptport.*;
import static mame.osdependH.*;
import static machine.gberet.*;
import static sndhrdw.gberet.*;
import static vidhrdw.generic.*;
import static vidhrdw.gberet.*;

public class gberet
{



	static MemoryReadAddress readmem[] =
	{
		new MemoryReadAddress( 0xc000, 0xe03f, MRA_RAM ),
		new MemoryReadAddress( 0x0000, 0xbfff, MRA_ROM ),
		new MemoryReadAddress( 0xf603, 0xf603, input_port_2_r ),	/* IN2 */
		new MemoryReadAddress( 0xf602, 0xf602, input_port_0_r ),	/* IN0 */
		new MemoryReadAddress( 0xf601, 0xf601, input_port_1_r ),	/* IN1 */
		new MemoryReadAddress( 0xf600, 0xf600, input_port_3_r ),	/* DSW1 */
		new MemoryReadAddress( 0xf200, 0xf200, input_port_4_r ),	/* DSW2 */
		new MemoryReadAddress( 0xf400, 0xf400, input_port_5_r ),	/* DSW3 */
		new MemoryReadAddress( -1 )	/* end of table */
	};
	
	static MemoryWriteAddress writemem[] =
	{
		new MemoryWriteAddress( 0xd000, 0xd0bf, MWA_RAM, spriteram,spriteram_size ),
		new MemoryWriteAddress( 0xd100, 0xd1bf, MWA_RAM, spriteram_2 ),
		new MemoryWriteAddress( 0xd200, 0xdfff, MWA_RAM ),
		new MemoryWriteAddress( 0xc000, 0xc7ff, colorram_w, colorram ),
		new MemoryWriteAddress( 0xc800, 0xcfff, videoram_w, videoram,videoram_size ),
		new MemoryWriteAddress( 0xe000, 0xe03f, MWA_RAM, gberet_scroll ),
		new MemoryWriteAddress( 0xe043, 0xe043, MWA_RAM, gberet_spritebank ),
		new MemoryWriteAddress( 0xe044, 0xe044, MWA_RAM, gberet_interrupt_enable ),
		new MemoryWriteAddress( 0xf200, 0xf200, gberet_sound1_w ),
		new MemoryWriteAddress( 0xf400, 0xf400, MWA_NOP ),
		new MemoryWriteAddress( 0xf000, 0xf000, MWA_NOP ),
		new MemoryWriteAddress( 0xf600, 0xf600, MWA_NOP ),
		new MemoryWriteAddress( 0x0000, 0xbfff, MWA_ROM ),
		new MemoryWriteAddress( -1 )	/* end of table */
	};

        static TrakPort[] trak_ports =
        { new TrakPort(-1) };

        static KEYSet[] keys =
        {
            new KEYSet(0, 2, "MOVE UP"),
            new KEYSet(0, 0, "MOVE LEFT"),
            new KEYSet(0, 1, "MOVE RIGHT"),
            new KEYSet(0, 3, "MOVE DOWN"),
            new KEYSet(0, 4, "KNIFE"),
            new KEYSet(0, 5, "FIRE"),
            new KEYSet(-1) };

		
	static InputPort input_ports[] =
	{
		new InputPort(	/* IN0 */
			0xff,
			new int[] { OSD_KEY_LEFT, OSD_KEY_RIGHT, OSD_KEY_UP, OSD_KEY_DOWN,
					OSD_KEY_CONTROL, OSD_KEY_ALT, 0, 0 }
		),
		new InputPort(	/* IN1 */
			0xff,
			new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }
		),
		new InputPort(	/* IN2 */
			0xff,
			new int[] { 0, 0, OSD_KEY_3, OSD_KEY_1, OSD_KEY_2, 0, 0, 0 }
		),
		new InputPort(	/* DSW1 */
			0xff,
			new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }
		),
		new InputPort(	/* DSW2 */
			0x7a,
			new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }
		),
		new InputPort(	/* DSW3 */
			0xff,
			new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }
		),
		new InputPort( -1 )	/* end of table */
	};


	
	static DSW dsw[] =
	{
		new DSW( 4, 0x03, "LIVES", new String[] { "7", "5", "3", "2" }, 1 ),
		new DSW( 4, 0x18, "BONUS", new String[] { "50000 200000", "50000 100000", "40000 80000", "30000 70000" }, 1 ),
		new DSW( 4, 0x60, "DIFFICULTY", new String[] { "HARDEST", "HARD", "MEDIUM", "EASY" }, 1 ),
		new DSW( 4, 0x80, "DEMO SOUNDS", new String[] { "ON", "OFF" }, 1 ),
		new DSW( -1 )
	};


	
	static GfxLayout charlayout = new GfxLayout
	(
		8,8,	/* 8*8 characters */
		512,	/* 512 characters */
		4,	/* 4 bits per pixel */
		new int[] { 0, 1, 2, 3 },	/* the four bitplanes are packed in one nibble */
		new int[] { 0*4, 1*4, 2*4, 3*4, 4*4, 5*4, 6*4, 7*4 },
		new int[] { 0*32, 1*32, 2*32, 3*32, 4*32, 5*32, 6*32, 7*32 },
		32*8	/* every char takes 8 consecutive bytes */
	);

	static GfxLayout spritelayout = new GfxLayout
	(
		16,16,	/* 16*16 sprites */
		256,	/* 256 sprites */
		4,	/* 4 bits per pixel */
		new int[] { 0, 1, 2, 3 },	/* the four bitplanes are packed in one nibble */
		new int[] { 0*4, 1*4, 2*4, 3*4, 4*4, 5*4, 6*4, 7*4,
			32*8+0*4, 32*8+1*4, 32*8+2*4, 32*8+3*4, 32*8+4*4, 32*8+5*4, 32*8+6*4, 32*8+7*4 },
		new int[] { 0*32, 1*32, 2*32, 3*32, 4*32, 5*32, 6*32, 7*32,
			64*8+0*32, 64*8+1*32, 64*8+2*32, 64*8+3*32, 64*8+4*32, 64*8+5*32, 64*8+6*32, 64*8+7*32 },
		128*8	/* every sprite takes 128 consecutive bytes */
	);


		
	static GfxDecodeInfo gfxdecodeinfo[] =
	{
		new GfxDecodeInfo( 1, 0x00000, charlayout,   16*16, 16 ),
		new GfxDecodeInfo( 1, 0x04000, spritelayout,     0, 16 ),
		new GfxDecodeInfo( 1, 0x0c000, spritelayout,     0, 16 ),
		new GfxDecodeInfo( -1 ) /* end of array */
	};


	
	static char color_prom[] =
	{
		/* 577h09 - palette */
		0x00,0x1A,0x26,0x1C,0xB6,0x74,0x0A,0x52,0xA4,0xD0,0xE8,0xAD,0x3F,0x06,0xFF,0x40,
		0x00,0x05,0x04,0x02,0x88,0x37,0xAA,0xAC,0x16,0xAE,0x24,0x10,0x52,0xA4,0xF6,0xFF,
		/* 577h10 - sprites */
		0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
		0x00,0x0D,0x0C,0x0F,0x00,0x0D,0x0C,0x0F,0x00,0x0D,0x0C,0x0F,0x00,0x0D,0x0C,0x0F,
		0x00,0x01,0x03,0x03,0x03,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x02,0x0F,
		0x00,0x06,0x02,0x04,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
		0x00,0x01,0x00,0x03,0x04,0x05,0x06,0x0E,0x00,0x09,0x0A,0x0B,0x07,0x0D,0x00,0x0F,
		0x00,0x01,0x02,0x0B,0x04,0x05,0x01,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
		0x00,0x01,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0C,0x0D,0x0F,0x0F,
		0x00,0x01,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x00,0x01,0x01,0x01,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x04,0x0F,
		0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0D,0x0B,0x0E,0x0F,
		0x00,0x0D,0x00,0x00,0x00,0x0D,0x0D,0x0D,0x0D,0x00,0x0D,0x0D,0x0D,0x00,0x00,0x00,
		0x00,0x05,0x00,0x00,0x00,0x05,0x05,0x05,0x05,0x00,0x05,0x05,0x05,0x00,0x00,0x00,
		0x00,0x00,0x02,0x03,0x04,0x09,0x06,0x04,0x02,0x09,0x0D,0x0E,0x0F,0x0D,0x0E,0x0F,
		0x00,0x00,0x02,0x03,0x04,0x09,0x06,0x07,0x08,0x09,0x0D,0x0E,0x0F,0x0D,0x0E,0x0F,
		0x00,0x0E,0x00,0x00,0x00,0x0E,0x0E,0x0E,0x0E,0x00,0x0E,0x0E,0x0E,0x00,0x00,0x00,
		0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0D,0x0A,0x0E,0x0F,
		/* 577h11 - characters */
		0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
		0x07,0x07,0x07,0x07,0x0E,0x0E,0x0E,0x0E,0x0D,0x0D,0x0D,0x0D,0x0C,0x0C,0x0C,0x0C,
		0x07,0x0F,0x0E,0x0D,0x07,0x0F,0x0E,0x0D,0x07,0x0F,0x0E,0x0D,0x07,0x0F,0x0E,0x0D,
		0x00,0x00,0x00,0x00,0x0E,0x0E,0x0E,0x0E,0x0D,0x0D,0x0D,0x0D,0x0C,0x0C,0x0C,0x0C,
		0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03,
		0x00,0x01,0x0B,0x08,0x00,0x01,0x0B,0x08,0x00,0x01,0x0B,0x08,0x00,0x01,0x0B,0x08,
		0x07,0x07,0x07,0x07,0x01,0x01,0x01,0x01,0x02,0x02,0x02,0x02,0x03,0x03,0x03,0x03,
		0x00,0x0D,0x04,0x0C,0x00,0x0D,0x04,0x0C,0x00,0x0D,0x04,0x0C,0x00,0x0D,0x04,0x0C,
		0x00,0x0D,0x0A,0x0B,0x00,0x0D,0x0A,0x0B,0x00,0x0D,0x0A,0x0B,0x00,0x0D,0x0A,0x0B,
		0x00,0x06,0x07,0x0C,0x00,0x06,0x07,0x0C,0x00,0x06,0x07,0x0C,0x00,0x06,0x07,0x0C,
		0x00,0x0F,0x0E,0x0D,0x00,0x0F,0x0E,0x0D,0x00,0x0F,0x0E,0x0D,0x00,0x0F,0x0E,0x0D,
		0x00,0x0E,0x0D,0x0C,0x00,0x0E,0x0D,0x0C,0x00,0x0E,0x0D,0x0C,0x00,0x0E,0x0D,0x0C,
		0x00,0x00,0x00,0x00,0x0A,0x0A,0x0A,0x0A,0x0B,0x0B,0x0B,0x0B,0x0C,0x0C,0x0C,0x0C,
		0x0D,0x0D,0x0D,0x0D,0x0E,0x0E,0x0E,0x0E,0x0D,0x0D,0x0D,0x0D,0x0C,0x0C,0x0C,0x0C,
		0x00,0x05,0x08,0x0E,0x00,0x05,0x08,0x0E,0x00,0x05,0x08,0x0E,0x00,0x05,0x08,0x0E,
		0x00,0x08,0x03,0x09,0x00,0x08,0x03,0x09,0x00,0x08,0x03,0x09,0x00,0x08,0x03,0x09
	};
	


	public static MachineDriver machine_driver = new MachineDriver
	(
		/* basic machine hardware */
		new MachineCPU[] {
			new MachineCPU(
				CPU_Z80,
				3072000,	/* 3.072 Mhz?? */
				0,
				readmem, writemem, null, null,
				gberet_interrupt, 16
			)
		},
		60,
		null,

		/* video hardware */
		32*8, 32*8, new rectangle( 1*8, 31*8-1, 1*8, 31*8-1 ),
		gfxdecodeinfo,
		32, 2*16*16,
		gberet_vh_convert_color_prom,
                VIDEO_TYPE_RASTER,
		null,
		gberet_vh_start,
		gberet_vh_stop,
		gberet_vh_screenrefresh,

		/* sound hardware */
		null,
		null,
		gberet_sh_start,
		gberet_sh_stop,
		gberet_sh_update
	);



	/***************************************************************************
	
	  Game driver(s)
	
	***************************************************************************/
	static RomLoadPtr gberet_rom= new RomLoadPtr(){ public void handler()  
        {
		ROM_REGION(0x10000);	/* 64k for code */
		ROM_LOAD( "c10_l03.bin",  0x0000, 0x4000, 0xae29e4ff );
		ROM_LOAD( "c08_l02.bin",  0x4000, 0x4000, 0x240836a5 );
		ROM_LOAD( "c07_l01.bin",  0x8000, 0x4000, 0x41fa3e1f );
              
		ROM_REGION(0x14000);	/* temporary space for graphics (disposed after conversion) */
		ROM_LOAD( "f03_l07.bin",  0x00000, 0x4000, 0x4da7bd1b );
		ROM_LOAD( "e05_l06.bin", 0x04000, 0x4000, 0x0f1cb0ca );
		ROM_LOAD( "e04_l05.bin", 0x08000, 0x4000, 0x523a8b66 );
		ROM_LOAD( "f04_l08.bin", 0x0c000, 0x4000, 0x883933a4 );
		ROM_LOAD( "e03_l04.bin", 0x10000, 0x4000, 0xccecda4c );
                ROM_END();
                
        }};
        static RomLoadPtr rushatck_rom= new RomLoadPtr(){ public void handler()  
        {
		ROM_REGION(0x10000);	/* 64k for code */
		ROM_LOAD( "rush_h03.10c", 0x0000, 0x4000, 0x4d276b52 );
		ROM_LOAD( "rush_h02.8c",  0x4000, 0x4000, 0xb5802806 );
		ROM_LOAD( "rush_h01.7c",  0x8000, 0x4000, 0xda7c8f3d );
             
		ROM_REGION(0x14000);	/* temporary space for graphics (disposed after conversion) */
		ROM_LOAD( "rush_h07.3f",  0x00000, 0x4000, 0x03f9815f );
		ROM_LOAD( "e05_l06.bin",  0x04000, 0x4000 ,0x0f1cb0ca );
		ROM_LOAD( "rush_h05.4e",  0x08000, 0x4000, 0x9d028e8f );
		ROM_LOAD( "f04_l08.bin",  0x0c000, 0x4000, 0x883933a4 );
		ROM_LOAD( "e03_l04.bin",  0x10000, 0x4000, 0xccecda4c );             
                ROM_END();            
        }};



	static HiscoreLoadPtr hiload = new HiscoreLoadPtr() { public int handler(String name)
	{
		/* check if the hi score table has already been initialized */
		if (memcmp(RAM, 0xd900, new char[] { 0x03, 0x30, 0x00 }, 3) == 0 &&
				memcmp(RAM, 0xd91b, new char[] { 0x01, 0x00, 0x00 }, 3) == 0)
		{
			FILE f;


			if ((f = fopen(name, "rb")) != null)
			{
				fread(RAM, 0xd900, 1, 6*10, f);
				RAM[0xdb06] = RAM[0xd900];
				RAM[0xdb07] = RAM[0xd901];
				RAM[0xdb08] = RAM[0xd902];
				fclose(f);
			}

			return 1;
		}
		else return 0;	/* we can't load the hi scores yet */
	} };



	static HiscoreSavePtr hisave = new HiscoreSavePtr() { public void handler(String name)
	{
		FILE f;


		if ((f = fopen(name, "wb")) != null)
		{
			fwrite(RAM, 0xd900, 1, 6*10, f);
			fclose(f);
		}
	} };



	public static GameDriver gberet_driver = new GameDriver
	(
                "Green Beret",
		"gberet",
                "NICOLA SALMORIA\nCHRIS HARDY\nPAUL SWAN",
		machine_driver,
	
		gberet_rom,
		null, null,
		null,
	
		input_ports,null, trak_ports, dsw, keys,
	
		color_prom, null, null,

		ORIENTATION_DEFAULT,
	
		hiload, hisave
	);

	public static GameDriver rushatck_driver = new GameDriver
	(
                "Rush'n Attack",
		"rushatck",
                "NICOLA SALMORIA\nCHRIS HARDY\nPAUL SWAN",
		machine_driver,
	
		rushatck_rom,
		null, null,
		null,
	
		input_ports,null,trak_ports, dsw, keys,
	
		color_prom, null, null,
		ORIENTATION_DEFAULT,
	
		hiload, hisave
	);
}

